﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TamTreSoSinh.DBContext;

namespace TamTreSoSinh.Admin
{
    public partial class SettingProductDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            LoadListOurService();

            int Id;
            bool IsValidId = int.TryParse(Request["Id"], out Id);
            if (IsValidId)
            {
                LoadData(Id);
            }
            else
            {
                // prepare create new
                txtPriority.Text = "1";
            }
        }

        private void LoadListOurService()
        {
            DBTamTreEntities dbContext = new DBTamTreEntities();
            List<ShoppingCategory> listShoppingCategory = dbContext.ShoppingCategories.OrderBy(obj => obj.Id).ToList();
            ddlServiceCategory.DataSource = listShoppingCategory;
            ddlServiceCategory.DataTextField = "Title";
            ddlServiceCategory.DataValueField = "Id";
            ddlServiceCategory.DataBind();
        }

        private void LoadData(int Id)
        {
            DBTamTreEntities dbContext = new DBTamTreEntities();

            Product product = dbContext.Products.FirstOrDefault(obj => obj.Id == Id);

            if (product == null)
            {
                return;
            }
            ddlServiceCategory.SelectedValue = product.ShoppingCategoryId.ToString();
            txtPriority.Text = product.Priority.ToString();
            txtTitle.Text = product.ProductName;
            txtOverview.Text = product.Overview;
            txtContent.Text = product.Description;
            txtImage.Text = product.ImagePath;
            if (product.OldPrice.HasValue)
            {
                txtOldPrice.Text = product.OldPrice.Value.ToString();
            }
            txtCurrentPrice.Text = product.CurrentPrice.ToString();
            ddlRate.SelectedValue = product.Rate.ToString();
            ddlIsOnHomePage.SelectedValue = product.IsOnHomePage.ToString();
            ddlIsOnCategory.SelectedValue = product.IsOnCategory.ToString();
            ddlIsOnStock.SelectedValue = product.IsInStock.ToString();
            ddlIsHot.SelectedValue = product.IsHot.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!IsValid)
            {
                return;
            }

            if (string.IsNullOrEmpty(Request["Id"]))
            {
                ProcessInsert();
            }
            else
            {
                ProcessUpdate();
            }

            Response.Redirect(@"/Admin/SettingProduct.aspx");
        }

        private void ProcessUpdate()
        {
            int Id;
            bool IsValidId = int.TryParse(Request["Id"], out Id);

            if (!IsValidId)
            {
                return;
            }

            DBTamTreEntities dbContext = new DBTamTreEntities();
            Product product = dbContext.Products.FirstOrDefault(obj => obj.Id == Id);

            if (product == null)
            {
                return;
            }

            product.ShoppingCategoryId = Convert.ToInt32(ddlServiceCategory.SelectedValue);
            product.Priority = Convert.ToInt32(txtPriority.Text);
            product.ProductName = txtTitle.Text;
            product.Overview = txtOverview.Text;
            product.Description = txtContent.Text;
            product.ImagePath = txtImage.Text;
            if (string.IsNullOrWhiteSpace(txtOldPrice.Text))
            {
                product.OldPrice = null;
            }
            else
            {
                product.OldPrice = Convert.ToInt32(txtOldPrice.Text);
            }
            product.CurrentPrice = Convert.ToInt32(txtCurrentPrice.Text);
            product.Rate = Convert.ToInt32(ddlRate.SelectedValue);
            product.IsOnHomePage = Convert.ToBoolean(ddlIsOnHomePage.SelectedValue);
            product.IsOnCategory = Convert.ToBoolean(ddlIsOnCategory.SelectedValue);
            product.IsInStock = Convert.ToBoolean(ddlIsOnStock.SelectedValue);
            product.IsHot = Convert.ToBoolean(ddlIsHot.SelectedValue);

            dbContext.SaveChanges();
        }

        private void ProcessInsert()
        {
            DBTamTreEntities dbContext = new DBTamTreEntities();
            Product product = new Product();

            product.ShoppingCategoryId = Convert.ToInt32(ddlServiceCategory.SelectedValue);
            product.Priority = Convert.ToInt32(txtPriority.Text);
            product.ProductName = txtTitle.Text;
            product.Overview = txtOverview.Text;
            product.Description = txtContent.Text;
            product.ImagePath = txtImage.Text;
            if (string.IsNullOrWhiteSpace(txtOldPrice.Text))
            {
                product.OldPrice = null;
            }
            else
            {
                product.OldPrice = Convert.ToInt32(txtOldPrice.Text);
            }
            product.CurrentPrice = Convert.ToInt32(txtCurrentPrice.Text);
            product.Rate = Convert.ToInt32(ddlRate.SelectedValue);
            product.IsOnHomePage = Convert.ToBoolean(ddlIsOnHomePage.SelectedValue);
            product.IsOnCategory = Convert.ToBoolean(ddlIsOnCategory.SelectedValue);
            product.IsInStock = Convert.ToBoolean(ddlIsOnStock.SelectedValue);
            product.IsHot = Convert.ToBoolean(ddlIsHot.SelectedValue);

            dbContext.Products.AddObject(product);
            dbContext.SaveChanges();
        }
    }
}